/*
  +----------------------------------------------------------------------+
  | PHP Version 7                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 1997-2016 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Author:                                                              |
  +----------------------------------------------------------------------+
*/


/* $Id$ */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ccsw.h"

int le_opencc;
//类定义
zend_class_entry *ccsw_ce;

//构造方法
PHP_METHOD(ccsw, __construct) {
	char *config;
	int config_len;

	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &config) == FAILURE) {
		return;
	}

	zend_update_property_stringl(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("_config"), config, config_len);
}


//析构方法
PHP_METHOD(ccsw, __destruct) {

}

//执行转换
PHP_METHOD(ccsw, convert) {

	zval *config;
	zval rv;
	opencc_t od;
	char *outstr;
	zend_string *str;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &str) == FAILURE) {
		return;
	}

	config = zend_read_property(ccsw_ce, getThis(), ZEND_STRL("_config"), 0, &rv);

	od = opencc_open(Z_STRVAL_P(config));
	if( od == (opencc_t) -1 ) {
		RETURN_FALSE;
	}

	outstr = opencc_convert_utf8(od, str->val, -1);
	int len = strlen(outstr);

	zend_string *ret = zend_string_alloc(len, 0);
	strncpy(ret->val, outstr, len + 1);
	opencc_convert_utf8_free(outstr);

	RETURN_STR(ret);
}

PHP_METHOD(ccsw, error) {
	php_printf("error called.");
}



/** {{{ ARG_INFO
 */
ZEND_BEGIN_ARG_INFO_EX(arg_ccsw_construct, 0, 0, 0)
	ZEND_ARG_INFO(0, config)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arg_ccsw_convert, 0, 0, 0)
	ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arg_ccsw_void, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */


/** {{{ ccsw_methods
*/
zend_function_entry ccsw_methods[] = {
	PHP_ME(ccsw, __construct, arg_ccsw_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
	PHP_ME(ccsw, __destruct, arg_ccsw_void, ZEND_ACC_PUBLIC)
	PHP_ME(ccsw, convert, arg_ccsw_convert, ZEND_ACC_PUBLIC)
	PHP_ME(ccsw, error, arg_ccsw_void, ZEND_ACC_PUBLIC)
	PHP_FE_END
};
/* }}} */



PHP_MINIT_FUNCTION(ccsw) { 
    zend_class_entry ccsw;
	INIT_CLASS_ENTRY(ccsw, "Ccsw", ccsw_methods);
    ccsw_ce = zend_register_internal_class_ex(&ccsw, NULL);
    zend_declare_property_null(ccsw_ce, ZEND_STRL("_config"), ZEND_ACC_PROTECTED TSRMLS_CC);

    le_opencc = zend_register_list_destructors_ex(NULL, NULL, "opencc_od", module_number);


	return SUCCESS;
}

zend_module_entry ccsw_module_entry =
{
    STANDARD_MODULE_HEADER,
    "ccsw",
    NULL,
    PHP_MINIT(ccsw),
    NULL, NULL, NULL, NULL,
    NO_VERSION_YET,
    STANDARD_MODULE_PROPERTIES,
};


#ifdef COMPILE_DL_CCSW
ZEND_GET_MODULE(ccsw)
#endif
